package actions.systools;

/**
 * 通常对于逻辑上是一组的业务操作，我们可以用一个DispatchAction类来处理这些操作，这样可以减少
 * Action类的数量并且可以公用某些代码（如检验）， 对于使用FormBean的Action来说这种方式有一个限制，就是“复用”的一组操作FormBean
 * 必须相同，因此 不建议采用DispatchAction，我们主张不用FormBean的Action采用这种方式减少Action的数量
 * 如域对象的列表(list)、删除、详细信息显示等操作可以用一个DispatchAction来实现，
 * 只要实现方法listXXX(...)、deleteXXX(...)、displayXXX()、displayXXXForUpdate（）等即可，
 * 更详细的使用说明请查询Struts文档
 */
import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.validator.DynaValidatorForm;

import service.DbMainService;

/**
 * 数据库的主要的Action对象，用于查询Hsql 、标准sql 、数据库信息 业务操作.
 * @author sunj.
 */
public class DbTableMainAction extends DispatchAction {

    /** dbMainService用来完成业务逻辑 .* */
    private DbMainService dbMainService = null;
    
    /**
     * 查询表结构的详细信息。
     * @param mapping struts mapping
     * @param form struts form
     * @param request http request
     * @param response http response
     * @return action forward
     * @throws Exception any execption
     */
    @SuppressWarnings("unchecked")
    public ActionForward dbTalbeDetailScreen(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        DynaValidatorForm tableForm = (DynaValidatorForm) form;
        String table = tableForm.getString("table");
        Collection<Object[]> tableDetail = new ArrayList<Object[]>();
        tableDetail = dbMainService.getDbTableDetail(table);
        request.setAttribute("TABLE_DETAIL", tableDetail);
        request.setAttribute("TABLE_NAME", table);
        request.setAttribute("COL_NUM", tableDetail.size());
        return mapping.findForward("Hsql.Screen");
    }

    /**
     * 执行查询语句。
     * @param mapping struts mapping
     * @param form struts form
     * @param request http request
     * @param response http response
     * @return action forward
     * @throws Exception any execption
     */
    public ActionForward excuteHsqlScreen(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        String hsql = request.getParameter("hsql");
        String type = request.getParameter("type");
        if("".equals(hsql) || null == hsql){
            request.setAttribute("ERRORS", "必须输入查询语句");
            return mapping.getInputForward();
        }
        ArrayList<Object[]> tableData = new ArrayList<Object[]>();
        String returnInfo = null;
        if ("hql".equals(type)) {
            returnInfo = dbMainService.getResultByHql(hsql, tableData);
        } else if ("sql".equals(type)) {
            returnInfo = dbMainService.getResultBySql(hsql, tableData);
        }
        if (null != returnInfo && returnInfo.startsWith("!!!")) {
            request.setAttribute("ERRORS", returnInfo);
            return mapping.getInputForward();
        }
        int columnSize = (tableData.size() > 0) ? tableData.get(0).length : 0;
        request.setAttribute("columnSize", columnSize);
        request.setAttribute("value", tableData);
        request.setAttribute("info", returnInfo);
        return mapping.findForward("Hsql.Screen");
    }

    /**
     * 查询数据库里的所有表。
     * @param mapping struts mapping
     * @param form struts form
     * @param request http request
     * @param response http response
     * @return action forward
     * @throws Exception any execption
     */
    @SuppressWarnings("unchecked")
    public ActionForward dbTableScreen(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Collection tableList = new ArrayList();
        tableList = dbMainService.getDbTable();
        request.getSession().setAttribute("TABLE_LIST", tableList);
        return mapping.findForward("Hsql.Screen");
    }


    /**
     * 生成sql语句。
     * @param mapping struts mapping
     * @param form struts form
     * @param request http request
     * @param response http response
     * @return action forward
     * @throws Exception any execption
     */
    @SuppressWarnings("unchecked")
    public ActionForward selectOfSqlScreen(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        String table = request.getParameter("table");
        String col = request.getParameter("colNum");
        String sqlType = request.getParameter("sqlType");
        String sql;
        Integer colNum =Integer.parseInt(col);
        String[] colValue = new String[colNum];
        for(Integer i=0;i<colNum;i++){
            colValue[i] = request.getParameter(i.toString());
        }
        if("create".equals(sqlType)){
            sql = dbMainService.getCreateOfSql(table,colNum,colValue);
        }else if("selectCount".equals(sqlType)){
            sql = dbMainService.getSelectCountOfSql(table,colNum,colValue);
        }else {
            sql = dbMainService.getSelectOfSql(table,colNum,colValue);
        }
        request.setAttribute("SQL", sql);
        return mapping.findForward("Hsql.Screen");
    }
    
    /**
     * @param pDbMainService the dbMainService to set
     */
    public void setDbMainService(DbMainService pDbMainService) {
        dbMainService = pDbMainService;
    }
}